package com.sayimo.maker.user.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sayimo.framework.exception.ServiceException;
import com.sayimo.maker.commons.Constant;
import com.sayimo.maker.user.dao.EnterpriseWalletIncomeExpendRecordInterfaceDao;
import com.sayimo.maker.user.dao.EnterpriseWalletInterfaceDao;
import com.sayimo.maker.user.dao.ProviderRefDao;
import com.sayimo.maker.user.service.EnterpriseWalletInterfaceService;
import com.sayimo.maker.user.vo.EnterpriseWallet;
import com.sayimo.maker.user.vo.EnterpriseWalletIncomeExpendRecord;
import com.sayimo.maker.user.vo.ProviderRef;

@Service("enterpriseWalletInterfaceService")
public class EnterpriseWalletServiceImpl implements EnterpriseWalletInterfaceService {
	
	private static Log logger = LogFactory.getLog(EnterpriseWalletServiceImpl.class);
	@Autowired
	private EnterpriseWalletIncomeExpendRecordInterfaceDao  enterpriseWalletIncomeExpendRecordInterfaceDao;
	@Autowired
	private ProviderRefDao  providerRefDao;
	@Autowired
	private EnterpriseWalletInterfaceDao  enterpriseWalletInterfaceDao;
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Map listEnterpriseWalletInOutDetail(int customerId,	Integer queryType, Integer rewardType, String orderType,
			Integer pageNow, Integer pageSize,String transferStatus) throws ServiceException {
		Map resultMap = new HashMap();
		Map  data = new HashMap();
		
		try {
			//通过会员id获取企业中间表id
			Map<String, Object> map = new HashMap<>();
			map.put("customerId", customerId);
			ProviderRef providerRef = providerRefDao.selectProviderRef(map);
			Integer providerRefId = providerRef.getId();
			
			Map<String, Object> paramMap = new HashMap<>();
			paramMap.put("providerRefId", providerRefId);
			paramMap.put("queryType", queryType);
			paramMap.put("rewardType", rewardType);
			if(orderType!=null){
				paramMap.put("orderType", orderType);
			}
			if(transferStatus!=null && !"".equals(transferStatus)){
				paramMap.put("transferStatus", Integer.valueOf(transferStatus));
			}
			if(pageNow != null && pageNow == 0){
				paramMap.put("pageNow", 0);
			}else if(pageNow != null && pageNow > 0){
				paramMap.put("pageNow", (pageNow-1)*pageSize);
			}
			if(pageSize != null){
				paramMap.put("pageSize", pageSize);
			}
			//获取交易记录
			List<EnterpriseWalletIncomeExpendRecord> expendRecords = enterpriseWalletIncomeExpendRecordInterfaceDao.queryWalletInOutByCond(paramMap);
			List<Map> dataList = new ArrayList<Map>();
			if (CollectionUtils.isEmpty(expendRecords)) {
				data.put("dataList",dataList);
				resultMap.put(Constant.STATUS, Constant.STATUS_SUCCESS);
				resultMap.put(Constant.DATA,data);
				return resultMap;
			}
			
			for (EnterpriseWalletIncomeExpendRecord incomeExpendRecord : expendRecords) {
				Map dataMap = new HashMap();
				dataMap.put("emsNo", incomeExpendRecord.getEmsNo());
				dataMap.put("createDate", incomeExpendRecord.getCreateDate().getTime());
				dataMap.put("inCome", incomeExpendRecord.getInCome() == null ? "" : incomeExpendRecord.getInCome());
				dataMap.put("exPend", incomeExpendRecord.getExPend() == null ? "" : incomeExpendRecord.getExPend());
				dataMap.put("commet", incomeExpendRecord.getCommet() == null ? "" :incomeExpendRecord.getCommet());
				dataMap.put("rewardType", incomeExpendRecord.getRewardType() == null ? "" : incomeExpendRecord.getRewardType());
				dataMap.put("fromType", incomeExpendRecord.getFromType());
				dataMap.put("providerName", incomeExpendRecord.getProviderName() == null ? "" :incomeExpendRecord.getProviderName());
				dataMap.put("alias", incomeExpendRecord.getAlias() == null ? "" :incomeExpendRecord.getAlias());
				dataMap.put("singleTurnover", incomeExpendRecord.getTurnover() == null ? "" :incomeExpendRecord.getTurnover());
				dataMap.put("ordersNo", incomeExpendRecord.getOrdersNo() == null ? "" :incomeExpendRecord.getOrdersNo());
				dataMap.put("mallType", incomeExpendRecord.getMallType() == null ? "" :incomeExpendRecord.getMallType());
				dataMap.put("transferStatus", incomeExpendRecord.getTransferStatus() == null ? "" :incomeExpendRecord.getTransferStatus());
				dataMap.put("failureCause", incomeExpendRecord.getFailureCause() == null ? "" :incomeExpendRecord.getFailureCause());
				
				dataList.add(dataMap);
			}
			data.put("dataList",dataList);	
			resultMap.put(Constant.STATUS, Constant.STATUS_SUCCESS);
			resultMap.put(Constant.DATA, data);
			return resultMap;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			throw new ServiceException(e);
		}
		
		
	}

}
